package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.kpx.app.R;
import com.kpx.app.adapter.WalletDetailListAdapter;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.Page;
import com.kpx.app.bean.WalletDetailListBean;
import com.kpx.app.bean.WalletFilterBean;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.uiutils.DisplayUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.wigdet.PopListSelect;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/3.
 * 相关钱包明细展示
 */

public class ActivityWalletDetailList extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.lv_show)
    ListView lvShow;
    @BindView(R.id.out_view)
    XRefreshView outView;
    @BindView(R.id.txt_empty_mention)
    TextView txtEmptyMention;
    @BindView(R.id.txt_time)
    TextView txtTime;
    @BindView(R.id.txt_type)
    TextView txtType;
    @BindView(R.id.txt_price)
    TextView txtPrice;
    private boolean isRefresh;
    private int pageNow = 1;
    /**
     * 1.积分2.可用资金3.冻结资金4.提现列表
     */
    public static int inType;
    public static final int TYPE_DETAIL_POINT = 0;
    public static final int TYPE_DETAIL_CANUSE = 1;
    public static final int TYPE_DETAIL_DONGJIE = 2;
    public static final int TYPE_DETAIL_WITH = 3;

    private List<WalletDetailListBean> wdlistbean = new ArrayList<>();
    private List<Object> menubeans = new ArrayList<>();
    private WalletDetailListAdapter adapter;
    private Page page;
    private String balanceType = "";
    private PopListSelect pop;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_wallet_detail_list);
        ButterKnife.bind(this);
        switch (inType) {
            case TYPE_DETAIL_POINT:
                UIUtils.setNormalTitle(titleview, "积分明细");
                txtPrice.setText("积分");
                break;
            case TYPE_DETAIL_CANUSE:
                UIUtils.setNormalTitle(titleview, "可用资金明细");
                break;
            case TYPE_DETAIL_DONGJIE:
                UIUtils.setNormalTitle(titleview, "冻结资金明细");
                break;
            case TYPE_DETAIL_WITH:
                UIUtils.setNormalTitle(titleview, "提现明细");
                break;
        }
        UIUtils.initRefreshView(outView);
        titleview.setRight("筛选", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pop == null) {
                    pop = new PopListSelect(ActivityWalletDetailList.this, menubeans, DisplayUtil.dip2px(ActivityWalletDetailList.this, 100), new PopListSelect.OnSelectClicListener() {
                        @Override
                        public void onClick(Object str) {
                            WalletFilterBean b = (WalletFilterBean) str;
                            balanceType = b.getValue() + "";
                            isRefresh = true;
                            pageNow = 1;
                            getData();
                        }
                    });
                }
                if (menubeans.size() == 0) {
                    getFilter();
                    return;
                }
                pop.setData(menubeans);
                pop.showAsDropDown(titleview.getRightView());
            }
        }, R.color.white);
        getFilter();
        getData();
    }

    /**
     * 获取筛选菜单数据
     */
    private void getFilter() {
        if (inType == TYPE_DETAIL_POINT) {
            WalletFilterBean b = new WalletFilterBean();
            b.setText("全部");
            b.setValue(0);
            WalletFilterBean b1 = new WalletFilterBean();
            b1.setText("收入");
            b1.setValue(1);
            WalletFilterBean b2 = new WalletFilterBean();
            b2.setText("支出");
            b2.setValue(2);
            menubeans.add(b);
            menubeans.add(b1);
            menubeans.add(b2);
            return;
        }
        String shortUrl = "";
        MyHashMap map = new MyHashMap();
        switch (inType) {
            case TYPE_DETAIL_POINT:
                shortUrl = HttpDataReqUrl.URL_POST_POINT_FILTER;
                break;
            case TYPE_DETAIL_CANUSE:
                shortUrl = HttpDataReqUrl.URL_POST_CANUSE_FILTER;

                break;
            case TYPE_DETAIL_DONGJIE:
                shortUrl = HttpDataReqUrl.URL_POST_DONGJIE_FILTER;
                break;
        }
        showLoadingDialog();
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    if (result.getData() != null) {
                        menubeans.addAll((ArrayList<WalletFilterBean>) result.getData());
                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityWalletDetailList.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivityWalletDetailList.this, error);
            }

        }, WalletFilterBean.class, HttpDataRequest.TYPE_LIST).POST(shortUrl, true, map);
    }

    @Override
    public void initData() {
        adapter = new WalletDetailListAdapter(ActivityWalletDetailList.this, wdlistbean, R.layout.item_wallet_show_list);
        lvShow.setAdapter(adapter);
        outView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                pageNow = 1;
                getData();// 获取数据
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                isRefresh = false;
                if (page != null && page.getCount() <= wdlistbean.size()) {
                    ToastUtil.getInstance().showToast(ActivityWalletDetailList.this, "没有更多了");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            outView.stopLoadMore();
                        }
                    }, 600);
                } else {
                    pageNow++;
                    getData();// 获取数据
                }
            }
        });

    }

    private void getData() {
        String shortUrl = "";
        MyHashMap map = new MyHashMap();
        switch (inType) {
            case TYPE_DETAIL_POINT:
                shortUrl = HttpDataReqUrl.URL_POST_POINT_DETAIL;
                map.put("balanceType", balanceType);
                break;
            case TYPE_DETAIL_CANUSE:
                shortUrl = HttpDataReqUrl.URL_POST_CANUSE_DETAIL;
                map.put("balanceType", balanceType);
                map.put("bankRollType", "1");
                break;
            case TYPE_DETAIL_DONGJIE:
                map.put("balanceType", balanceType);
                map.put("bankRollType", "2");
                shortUrl = HttpDataReqUrl.URL_POST_DONGJIE_DETAIL;
                break;
            case TYPE_DETAIL_WITH:
                shortUrl = HttpDataReqUrl.URL_POST_WITH_DETAIL;
                break;
        }
        showLoadingDialog();
        map.put("pageIndex", pageNow + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (isRefresh) {
                    outView.stopRefresh();
                } else {
                    outView.stopLoadMore();
                }
                if (result.getCode() == 0) {
                    if (result.getData() != null) {
                        if (isRefresh) {
                            wdlistbean.clear();
                        }
                        wdlistbean.addAll((ArrayList<WalletDetailListBean>) result.getData());
                        adapter.setData(wdlistbean);
                        page = result.getPage();
                        if (wdlistbean.size() == 0) {
                            txtEmptyMention.setVisibility(View.VISIBLE);
                        } else {
                            txtEmptyMention.setVisibility(View.GONE);
                        }
                    }

                } else {
                    ToastUtil.getInstance().showToast(ActivityWalletDetailList.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                if (isRefresh) {
                    outView.stopRefresh();
                } else {
                    outView.stopLoadMore();
                }
                ToastUtil.getInstance().showToast(ActivityWalletDetailList.this, error);
            }

        }, WalletDetailListBean.class, HttpDataRequest.TYPE_LIST).POST(shortUrl, true, map);
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
